//
//  InvadersAppDelegate.m


#import "InvadersAppDelegate.h"
//#import "WebStatistic.h"
#import "game.h"
#import "menu.h"
//#import "gameover.h"
//#import "vars.h"
#import <iostream>
#import "RootViewController.h"

NSString* playerName;
bool soundPresent;
// число побед
int blackVictoryCount;
int whiteVictoryCount;

//стартовая линия
int blackStartLine;
int whiteStartLine;

//последняя игра
int lastGameBlackCount;
int lastGameWhiteCount;

int quick_game;
int gameProhod;// номер прохода до победы


bool first_run;
int numPlayers;

int level_cnt;

int total_score1;
int total_score2;
int g_score1;
int g_score2;

int g_color;
int diffic;

int mode;

UIView *Xview;

@implementation InvadersAppDelegate

@synthesize window;
@synthesize animationTimer;
@synthesize animationInterval;

//=============================================================
// Проверяем, есть ли файл со скорами, если нет - создаем
//
void initScoreFile()
{
	long f = xReadFile("file://score.dat");
	if (f == 0){
		//файла нет - пишем данные по-умолчанию
		char names[SCORE_COUNT][50];
		int scores[SCORE_COUNT];
		strcpy(names[0], "HyperZLO");  scores[0] = 99999;
		strcpy(names[1], "Necrofildoza"); scores[1] = 90000;
		strcpy(names[2], "Mega-zavr"); scores[2] = 80000;
		strcpy(names[3], "PartiZZan"); scores[3] = 75000;
		strcpy(names[4], "Looser"); scores[4] = 67000;
		f=xWriteFile("file://score.dat");
		for (int i=0; i< SCORE_COUNT; i++){
			xWriteLine(f, names[i]);
			xWriteInt(f, scores[i]);
		}
		xCloseFile(f);
		f = NULL;
	}
	
}

- (void)applicationDidFinishLaunching:(UIApplication *)application
{	
    //[FlurryAnalytics startSession:@"PPN6ZI9FGCFE45IFN3PN"];


	initScoreFile();
    // disable status bar
    [application setStatusBarHidden:YES];
    
    window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
    [window makeKeyAndVisible];
    
    //RootViewController* vc = [[RootViewController alloc] init];
    //[window addSubview:vc.view];
    
    RootViewControllerX * vc = [[RootViewControllerX alloc] init];
    [window setRootViewController:vc];
    
    /*
	// initialize engine
	xGraphics3D(0,true, window);

	// start menu stage
	MenuStage * gameStage = new MenuStage();//GameStage();
	gameStage->Load();
	// start drawing
	animationInterval = 1.0 / 120.0;
	[self startAnimation];
     */
}

- (void)drawView
{
	// check stage
	if(Stage::GetActive() == NULL) return;
	// update stage
	Stage::GetActive()->Update();
	// render stage
	Stage::GetActive()->Render();
}

- (void)layoutSubviews
{
    xResetGraphics();
    [self drawView];
}

- (void)startAnimation
{
    self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES];
}

- (void)stopAnimation
{
    self.animationTimer = nil;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
	animationInterval = 1.0 / 5.0;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
	animationInterval = 1.0 / 120.0;
}

- (void)dealloc
{
	[window release];
	[super dealloc];
}

@end